package interfaces;

import java.util.ArrayList;
import negocio.Contato;
import com.example.falai.R;
import databases.ContatosDS;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class ContatosActivity extends Activity {
	Button buscar;
	private ContatosDS contato;
	ArrayList<Contato>contatoArrayList;
	ArrayAdapter<Contato> contatoAdapter;
	ListView listaContatos;
	private String curNick;
	private Bundle extras;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_contatos);
		addListenerOnButton();

		extras = getIntent().getExtras();
		
		if(extras != null)
		{
			curNick = extras.getString("user");
		}
		
		contato = new ContatosDS(this);
		contato.open();
		
		contato.atualizaMapaContatos(curNick);
		
		listaContatos = (ListView)findViewById(R.id.lv_contatos);
		registerForContextMenu(listaContatos);
		contatoArrayList = contato.getContatosList();
		contatoAdapter = new ArrayAdapter<Contato>(this, android.R.layout.simple_list_item_1, contatoArrayList);
		listaContatos.setAdapter(contatoAdapter);
		
		listaContatos.setOnItemClickListener(new OnItemClickListener() {
			  @Override
			  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			    //TODO
				  	Intent intent = new Intent(ContatosActivity.this, MensagemActivity.class);
					intent.putExtra("nickContato", contatoAdapter.getItem(position).getNick());
					intent.putExtra("user", curNick);
					startActivity(intent);
					
					//return true;
				  
				  
			  }
			});
	
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.contatos, menu);
		return true;
	}

	public void addListenerOnButton() {
		
		buscar = (Button) findViewById(R.id.buscarContato);
		buscar.setOnClickListener(new OnClickListener() {
			
 	@Override
	public void onClick(View arg0) {
 	 	Intent intent= new Intent(ContatosActivity.this, BuscarContatosActivity.class);
		intent.putExtra("user", curNick);
 	 	
 	 	ContatosActivity.this.startActivity(intent);
	
 		}
		
    });
	
}
	
	
}
